package com.yuanlrc.base.controller.admin;

import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.entity.common.GoodsCategory;
import com.yuanlrc.base.service.common.GoodsCategoryService;
import com.yuanlrc.base.util.ValidateEntityUtil;
import freemarker.ext.beans.StringModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.ArrayList;

/*
*后台物品分类管理控制器
*  */
@RequestMapping("/admin/goods_category")
@Controller
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;


    /**
     * 物品分类管理列表页面
     * @param
     * @param pageBean
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(GoodsCategory goodsCategory, PageBean<GoodsCategory> pageBean, Model model){
        model.addAttribute("title","物品分类列表");
        model.addAttribute("name",goodsCategory.getName());
        model.addAttribute("pageBean",goodsCategoryService.findlist(pageBean,goodsCategory));
        return "admin/goods_category/list";
    }
    @GetMapping(value = "/add")
    public String add(Model model){
        model.addAttribute("title","添加物品分类");
        model.addAttribute("goodsCategorys",goodsCategoryService.findTopCategorys());
        return "admin/goods_category/add";
    }

    /**
     * 商品分类添加表单提交
     * @param goodsCategory
     * @return
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(GoodsCategory goodsCategory) {
        if (goodsCategory == null) {
            return Result.error(CodeMsg.DATA_ERROR);
        }
        CodeMsg validate = ValidateEntityUtil.validate(goodsCategory);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(goodsCategory.getParent()!=null && goodsCategory.getParent().getId()==null){
            goodsCategory.setParent(null);
        }
        if(goodsCategoryService.save(goodsCategory)==null){
            return Result.error(CodeMsg.ADMIN_GOODSCATEGORY_ADD_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 编辑商品分类页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping(value = "/edit")
    public String add(@RequestParam(name = "id",required = true) Long id, Model model){
        model.addAttribute("title","添加物品分类");
        model.addAttribute("goodsCategory",goodsCategoryService.findById(id));
        model.addAttribute("goodsCategorys",goodsCategoryService.findTopCategorys());
        return "admin/goods_category/edit";
    }
    /**
     * 商品分类修改表单提交
     * @param goodsCategory
     * @return
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public Result<Boolean> edit(GoodsCategory goodsCategory) {
        if (goodsCategory == null) {
            return Result.error(CodeMsg.DATA_ERROR);
        }
        CodeMsg validate = ValidateEntityUtil.validate(goodsCategory);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(goodsCategory.getParent()!=null && goodsCategory.getParent().getId()==null){
            goodsCategory.setParent(null);
        }
        if(goodsCategory.getId()==null){
            return Result.error(CodeMsg.DATA_ERROR);
        }
        GoodsCategory existGoodsCategory = goodsCategoryService.findById(goodsCategory.getId());
        if(existGoodsCategory==null){
            return Result.error(CodeMsg.DATA_ERROR);
        }
        existGoodsCategory.setIcon(goodsCategory.getIcon());
        existGoodsCategory.setParent(goodsCategory.getParent());
        existGoodsCategory.setName(goodsCategory.getName());
        existGoodsCategory.setSort(goodsCategory.getSort());
        if(goodsCategoryService.save(existGoodsCategory)==null){
            return Result.error(CodeMsg.ADMIN_GOODSCATEGORY_EDIT_ERROR);
        }
        return Result.success(true);
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id",required = true) Long id) {
        try {
            goodsCategoryService.delete(id);
        }catch (Exception e){
            return Result.error(CodeMsg.ADMIN_GOODSCATEGORY_DELETE_ERROR);
        }
        return Result.success(true);
    }
}
